﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace KouDai.Phone.ViewModels
{
    public class SecondCategoryPageViewModel : KouDaiScreen
    {
        
        #region Fields
        private readonly INavigationService _navigationService;
        private ObservableCollection<GroupCollection<SearchCategoryEntity>> _items = new ObservableCollection<GroupCollection<SearchCategoryEntity>>();
        #endregion

        #region Constructor
        public SecondCategoryPageViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public SecondCategoryPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
        public ObservableCollection<GroupCollection<SearchCategoryEntity>> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.NotifyOfPropertyChange(() => this.Items);
                }
            }
        }

        public string CategoryId { get; set; }
        public string Title { get; set; }
        #endregion

        #region Override
        public override void LoadDesignData()
        {
            List<SearchCategoryEntity> _secondLevelItems = new List<SearchCategoryEntity>();

            SearchCategoryEntity searchA = new SearchCategoryEntity { Id = "322", ItemTagName = "上衣", ImageUrl = "http://img.geilicdn.com/4fde2c4dc9b3335110c111a8b4a3765d_200x200.jpg", ParentId = "321" };
            SearchCategoryEntity subA1 = new SearchCategoryEntity { Id = "325", ItemTagName = "背心背心背心", ImageUrl = "http://img.geilicdn.com/a42148eb32c1dfa0cebf68917554e0d6_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA2 = new SearchCategoryEntity { Id = "326", ItemTagName = "衬衫", ImageUrl = "http://img.geilicdn.com/85e54a74ebc80475fceae9570850452e_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA3 = new SearchCategoryEntity { Id = "327", ItemTagName = "打底衫", ImageUrl = "http://img.geilicdn.com/26900caf7f7c9c16f592c53a9f3ca374_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA4 = new SearchCategoryEntity { Id = "328", ItemTagName = "马甲", ImageUrl = "http://img.geilicdn.com/2a6d39538780785613d77a7c6b9e8b9c_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA5 = new SearchCategoryEntity { Id = "329", ItemTagName = "POLO衫", ImageUrl = "http://img.geilicdn.com/0d3c8e00c1e49414b439980e98b92d25_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA6 = new SearchCategoryEntity { Id = "330", ItemTagName = "T恤", ImageUrl = "http://img.geilicdn.com/2b5d0f242e584403a7387e1444b26c29_200x200.jpg", ParentId = "322" };
            SearchCategoryEntity subA7 = new SearchCategoryEntity { Id = "331", ItemTagName = "西装", ImageUrl = "http://img.geilicdn.com/837ddb27931bdc10ae765a7922b28067_200x200.jpg", ParentId = "322" };
            searchA.SubCategories.Add(subA1);
            searchA.SubCategories.Add(subA2);
            searchA.SubCategories.Add(subA3);
            searchA.SubCategories.Add(subA4);
            searchA.SubCategories.Add(subA5);
            searchA.SubCategories.Add(subA6);
            searchA.SubCategories.Add(subA7);
            _secondLevelItems.Add(searchA);

            SearchCategoryEntity searchB = new SearchCategoryEntity { Id = "323", ItemTagName = "裤子", ImageUrl = "http://img.geilicdn.com/1d04bf6560d5ee60b9549f6944d9e2b5_200x200.jpg", ParentId = "321" };
            SearchCategoryEntity subB1 = new SearchCategoryEntity { Id = "332", ItemTagName = "背带裤", ImageUrl = "http://img.geilicdn.com/fac2a0e36e29eee5f4cd01d1e6509191_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB2 = new SearchCategoryEntity { Id = "333", ItemTagName = "短裙", ImageUrl = "http://img.geilicdn.com/7a047d4b4274360d77b0d35268fe7ac8_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB3 = new SearchCategoryEntity { Id = "334", ItemTagName = "工装裤", ImageUrl = "http://img.geilicdn.com/3600a53db577cc80f590f2d3fdd01117_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB4 = new SearchCategoryEntity { Id = "335", ItemTagName = "哈伦裤", ImageUrl = "http://img.geilicdn.com/645b6f404613710dfc5306e605492cc9_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB5 = new SearchCategoryEntity { Id = "338", ItemTagName = "牛仔裤", ImageUrl = "http://img.geilicdn.com/8c2108429d683e97a4f9244e5a3a8a35_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB6 = new SearchCategoryEntity { Id = "339", ItemTagName = "皮裤", ImageUrl = "http://img.geilicdn.com/72244881f306cfafd5580af903a1d74c_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB7 = new SearchCategoryEntity { Id = "340", ItemTagName = "七分裤", ImageUrl = "http://img.geilicdn.com/46b0a509ef9193df65b3f516c6c58674_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB8 = new SearchCategoryEntity { Id = "341", ItemTagName = "五分裤", ImageUrl = "http://img.geilicdn.com/afc53e926883c90d5a7d59e64f0a6e73_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB9 = new SearchCategoryEntity { Id = "342", ItemTagName = "小脚裤", ImageUrl = "http://img.geilicdn.com/11e5996520c26a9c3d34ab4c903ffeb2_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB10 = new SearchCategoryEntity { Id = "343", ItemTagName = "西装裤", ImageUrl = "http://img.geilicdn.com/06940b39a74bd58eb7f2ee53bb365494_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB11 = new SearchCategoryEntity { Id = "344", ItemTagName = "运动裤", ImageUrl = "http://img.geilicdn.com/3c6b82f8a2a392b5ac881e823992358c_200x200.jpg", ParentId = "323" };
            SearchCategoryEntity subB12 = new SearchCategoryEntity { Id = "345", ItemTagName = "直筒裤", ImageUrl = "http://img.geilicdn.com/5396e505e2c58de1c2b0ae31106c5226_200x200.jpg", ParentId = "323" };

            searchB.SubCategories.Add(subB1);
            searchB.SubCategories.Add(subB2);
            searchB.SubCategories.Add(subB3);
            searchB.SubCategories.Add(subB4);
            searchB.SubCategories.Add(subB5);
            searchB.SubCategories.Add(subB6);
            searchB.SubCategories.Add(subB7);
            searchB.SubCategories.Add(subB8);
            searchB.SubCategories.Add(subB9);
            searchB.SubCategories.Add(subB10);
            searchB.SubCategories.Add(subB11);
            searchB.SubCategories.Add(subB12);
            _secondLevelItems.Add(searchB);

            foreach (SearchCategoryEntity search in _secondLevelItems)
            {
                GroupCollection<SearchCategoryEntity> group = new GroupCollection<SearchCategoryEntity>(search.ItemTagName);

                foreach (var subSearch in search.SubCategories)
                {
                    group.Add(subSearch);
                }
                Items.Add(group);
            }
        }

        protected override void OnInitialize()
        {
            GetSecondLevelData();
        }
        #endregion

        #region Method
        private async void GetSecondLevelData()
        {
            BackData backData = await ServiceManager.GetSearchCategory(CategoryId);
            if (backData.IsSuccess)
            {
                List<SearchCategoryEntity> categoryItems = (List<SearchCategoryEntity>)backData.ResposeData;
                this.Items = TransferLongListCollection(categoryItems);
            }
            else
            {
                Debug.WriteLine("msg:" + backData.Msg);
            }
        }

        private ObservableCollection<GroupCollection<SearchCategoryEntity>> TransferLongListCollection(List<SearchCategoryEntity> categoryItems)
        {
            ObservableCollection<GroupCollection<SearchCategoryEntity>> transferItems = new ObservableCollection<GroupCollection<SearchCategoryEntity>>();
            foreach (SearchCategoryEntity search in categoryItems)
            {
                GroupCollection<SearchCategoryEntity> group = new GroupCollection<SearchCategoryEntity>(search.ItemTagName);
                foreach (var subSearch in search.SubCategories)
                {
                    group.Add(subSearch);
                }
                transferItems.Add(group);
            }
            return transferItems;
        }
        #endregion

        #region ViewMethod
        public void SelectCategory(SearchCategoryEntity searchItem)
        {
            Debug.WriteLine("选择的searchcategory: " + searchItem.Id + "----" + searchItem.ItemTagName);
        }

      
        #endregion

    }
}
